package com.nian.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 用途：数据库模型生成器
 * 开发：zhangmj
 * 日期：2020/3/28 17:23
 */
public class MyBatisPlusGeneratorByModule {

    public static final String NIAN_BASE_MODEL = "nian-user-center";
    // model 模块
    public static final String NIAN_MODEL = NIAN_BASE_MODEL + "/nian-user-model";
    // dao 模块
    public static final String NIAN_DAO = NIAN_BASE_MODEL +  "/nian-user-dao";
    // service 模块
    public static final String NIAN_DAO_SERVICE = NIAN_BASE_MODEL +  "/nian-user-service";

    public static void main(String[] args) {
        String[] modelArray = {NIAN_MODEL, NIAN_DAO, NIAN_DAO_SERVICE};
        for (String modelName:modelArray) {
            createByModel(modelName);
        }
    }

    public static void createByModel(String projectModel) {
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        System.out.println("目录：" + projectModel);
        String projectPath = projectModel + "/src/main/java";
        System.out.println("目录：" + projectPath);
        gc.setOutputDir(projectPath);
        // 文件覆盖
        gc.setFileOverride(false);
        gc.setAuthor("zhangmj");
        // 生成后打开文件夹;
        gc.setOpen(false);
        gc.setSwagger2(true);
        // 生成基本的resultMap
        gc.setBaseResultMap(true);
        // 生成基本的SQL片段
        gc.setBaseColumnList(true);
        // 主键自动返回
        gc.setIdType(IdType.AUTO);
        // 设置日期
        gc.setActiveRecord(true);
        gc.setEnableCache(true);


        String url = "jdbc:mysql://127.0.0.1:3306/nian_user_center?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("nian");
        dsc.setPassword("nian");
        // 关闭默认生成，如果设置空 OR Null 将不生成该模块。
        TemplateConfig templateConfig = new TemplateConfig();
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.nian.user")
                .setEntity("model")
                .setMapper("dao")
                .setXml("dao.mapping")
                .setService("center.service")
                .setServiceImpl("center.service.impl");
        if (NIAN_MODEL.equals(projectModel)) {
            templateConfig.setXml(null).setMapper(null).setService(null).setServiceImpl(null).setController(null);
        }else if(NIAN_DAO.equals(projectModel)){
            templateConfig.setEntity(null).setService(null).setServiceImpl(null).setController(null);
        }else if(NIAN_DAO_SERVICE.equals(projectModel)){
            templateConfig.setEntity(null).setXml(null).setMapper(null).setController(null);
        }else {
            throw new MybatisPlusException("请选择生成模型的类型 ***** ");
        }

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 表名生成策略(下划线转驼峰命名)
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 列名生成策略(下划线转驼峰命名)
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // 是否启动Lombok配置
        strategy.setEntityLombokModel(true);
        // 是否启动REST风格配置
        strategy.setRestControllerStyle(true);

        // 需要生成的表, 不配置则默认数据库内全部数据
        String[] tables = {"app_user"};
        strategy.setInclude(tables);
        // 自动生成
        AutoGenerator mpg = new AutoGenerator();
        mpg.setDataSource(dsc);
        mpg.setGlobalConfig(gc);
        mpg.setStrategy(strategy);
        mpg.setPackageInfo(pc);
        mpg.setTemplate(templateConfig);
        // 执行
        mpg.execute();
    }
}
